/*
 * NoRacleSales.java
 *
 * Created on April 20, 2007, 1:44 PM
 *
 * NoRacleSales is the DB that contains
 * the SalesRecords objects. 
 */

package salesDB;

import java.util.Date;
import java.util.LinkedList;
import java.util.ListIterator;

public class NoRacleSales
{
    // DB is implemented as a linked list
    private LinkedList saleList = new LinkedList ();
    
    /** Creates a new instance of NoRacleSales */
    public NoRacleSales ()
    {
        System.out.println ("Creating NoRacle Sales Database!");
    }
    
    public boolean addRecord (SalesRecord rec)
    {
        boolean status = saleList.add (rec);
        if (status)
            System.out.println ("Adding Record to NoRacle Sales Database!");
        return status;
    }
    
    public boolean deleteRecord (Date date, int id)
    {
        SalesRecord rec;
        ListIterator it = saleList.listIterator ();
        boolean status = false;
        
        while( it.hasNext ())
        {
            rec = (SalesRecord)it.next ();
            if( rec.date.equals (date) && rec.transactionID == id )
            {
                status = true;
                System.out.println ("Successful delete.");
                it.remove ();
            }
        }
        if (!status)
            System.out.println ("Could not find sale record to delete, Date: " + date.toString () + ", Id: " + id);
        return status;
    }
    
    public SalesRecord getRecord (Date date, int id)
    {
        SalesRecord rec = null;
        ListIterator it = saleList.listIterator ();
        boolean status = false;
        
        while( it.hasNext ())
        {
            rec = (SalesRecord)it.next ();
            if( rec.date.equals (date) && rec.transactionID == id )
            {
                status = true;
                System.out.println ("Getting Record from NoRacle Sales Database.");
                break;
            }
        }
        
        if (!status)
            System.out.println ("Could not find sale record, Date: " + date.toString () + ", Id: " + id);
        
        return rec;
    }
    
    public LinkedList getRecsByDate (Date date)
    {
        LinkedList dateList = new LinkedList ();
        SalesRecord rec = null;
        ListIterator it = saleList.listIterator ();
        ///Date today = new Date();
        
        while( it.hasNext ())
        {
            rec = (SalesRecord)it.next ();
            if( /*rec.date.equals (date)*/ true ) // TODO: implement date filtering
            {
                dateList.add (rec);
            }
        }    
        return dateList;        
    }
    
    public int databaseSize()
    {
        return saleList.size ();
    }
    
}
